package com.lxmes.gs.controller;

import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import com.lxmes.gs.domain.taskIssueVO.*;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.lxmes.gs.domain.TaskIssue;
import com.lxmes.gs.service.ITaskIssueService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;

/**
 * 任务下发Controller
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Api(tags = "任务下发")    
@RestController
@RequestMapping("/gs/taskIssue")
public class TaskIssueController extends BaseController{
    @Autowired
    private ITaskIssueService taskIssueService;

    /**
     * 查询任务下发列表
     */
    @ApiOperation("查询任务下发列表")
    @PreAuthorize("@ss.hasPermi('gs:taskIssue:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaskIssueSearchVO taskIssue){
        startPage();
        List<TaskIssue> list = taskIssueService.selectTaskIssueList(BeanUtil.copyProperties(taskIssue,TaskIssue.class));
        return getDataTable(Convert.convert(new TypeReference<List<TaskIssueResultVO>>() {},list));
}

    /**
     * 导出任务下发列表
     */
    @ApiOperation("导出任务下发列表")
    @PreAuthorize("@ss.hasPermi('gs:taskIssue:export')")
    @Log(title = "任务下发", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaskIssue taskIssue){
        List<TaskIssue> list = taskIssueService.selectTaskIssueList(taskIssue);
        List<TaskIssueExportVO> exportList = Convert.convert(new TypeReference<List<TaskIssueExportVO>>() {}, list);
        ExcelUtil<TaskIssueExportVO> util = new ExcelUtil<TaskIssueExportVO>(TaskIssueExportVO.class);
        util.exportExcel(response, exportList, "任务下发数据");
    }

    /**
     * 获取任务下发详细信息
     */
    @ApiOperation("获取任务下发详细信息")
    @PreAuthorize("@ss.hasPermi('gs:taskIssue:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return AjaxResult.success(taskIssueService.selectTaskIssueById(id));
    }

    /**
     * 新增任务下发
     */
    @ApiOperation("新增任务下发")
    @PreAuthorize("@ss.hasPermi('gs:taskIssue:add')")
    @Log(title = "任务下发", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaskIssueCreateVO taskIssue){
        return toAjax(taskIssueService.insertTaskIssue(BeanUtil.copyProperties(taskIssue,TaskIssue.class)));
    }

    /**
     * 修改任务下发
     */
    @ApiOperation("修改任务下发")
    @PreAuthorize("@ss.hasPermi('gs:taskIssue:edit')")
    @Log(title = "任务下发", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaskIssueUpdateVO taskIssue){
        return toAjax(taskIssueService.updateTaskIssue(BeanUtil.copyProperties(taskIssue,TaskIssue.class)));
    }

    /**
     * 删除任务下发
     */
    @ApiOperation("删除任务下发")
    @PreAuthorize("@ss.hasPermi('gs:taskIssue:remove')")
    @Log(title = "任务下发", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(taskIssueService.deleteTaskIssueByIds(ids));
    }
    /**
     * 任务下发审核
     */
    @ApiOperation("任务下发审核")
    @PreAuthorize("@ss.hasPermi('gs:taskIssue:audit')")
    @Log(title = "任务下发审核")
	@PostMapping("/auditTaskIssue/{ids}")
    public AjaxResult auditTaskIssue(@PathVariable Long[] ids){
        return toAjax(taskIssueService.auditTaskIssue(ids));
    }
    /**
     * 导入任务下发
     */
    @Log(title = "任务下发", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception{
        ExcelUtil<TaskIssueImportVO> util = new ExcelUtil<>(TaskIssueImportVO.class);
        List<TaskIssueImportVO> taskIssueList = util.importExcel(file.getInputStream());
        String message = taskIssueService.imporIteminfo(Convert.convert(new TypeReference<List<TaskIssue>>() {},taskIssueList), updateSupport);
        return AjaxResult.success(message);
    }
    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException{
        ExcelUtil<TaskIssueImportVO> util = new ExcelUtil<>(TaskIssueImportVO.class);
        util.importTemplateExcel(response,"任务下发");
    }
}
